package testcase;

import static org.junit.Assert.*;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import control.ReadFile;

public class ReadFileTest{	
	
	private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
	private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();

	@Before
	public void setUpStreams() {
	    System.setOut(new PrintStream(outContent));
	    System.setErr(new PrintStream(errContent));
	}

	@After
	public void cleanUpStreams() {
	    System.setOut(null);
	    System.setErr(null);
	}	
	
	@Test
	public void testCantOpenFile()
	{
		//String result=readFile.toString();hahahah
		//System.out.println("result");
		ReadFile readFile = new ReadFile("C:\\test.txt");
		readFile.readItemFile();
		assertEquals(true,outContent.toString().trim().equals("Error!"));
	}
	
	@Test
	public void testReadItemFile(){
		
		ReadFile file = new ReadFile("/testFile/itemTest.txt");
		file.readItemFile();
		assertEquals("test",file.getResult().get(0));
		assertEquals("test2",file.getResult().get(3));
		assertEquals("ImagePath2",file.getResult().get(5));
		int result = file.getEffect().get(0);
		assertEquals(1,result);
		
		assertEquals(2,file.getEffect().size());
		assertEquals(6,file.getResult().size());
	}
	
	@Test
	public void testReadMCFile(){
		
		ReadFile file= new ReadFile("/testFile/MCTest.txt");
		file.readMCFile();
		
		//option testing
		assertEquals("test",file.getResult().get(0));
		assertEquals("option2",file.getResult().get(2));
		assertEquals("option4",file.getResult().get(4));
		
		//effect testing
		for(int i=0;i<4;i++){
			
			int result = file.getEffect().get(i);
			//String expect=i+1;
			assertEquals(i+1,result);
		}
	}
	
	@Test
	public void testReadNormalCardFile(){
		ReadFile file =new ReadFile("/testFile/Understand.txt");
		file.readNormalCardFile();
		
		for(int i=0;i<3;i++){
			String result=file.getResult().get(i);
			assertEquals("test"+(i+1),result);
		}
		
		for(int i=0;i<3;i++){
			int effect=file.getEffect().get(i);
			assertEquals(i+1,effect);
		}
	}
}
	

